package dj.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dj.dao.BaseDao;
import dj.model.SysRoleMembers;
import dj.model.SysRoles;
import dj.service.SysRoleService;

@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

	BaseDao<SysRoles> sysRolesDao;

	BaseDao<SysRoleMembers> sysRoleMemberDao;

	public BaseDao<SysRoles> getSysRolesDao() {
		return sysRolesDao;
	}

	@Autowired
	public void setSysRolesDao(BaseDao<SysRoles> sysRolesDao) {
		this.sysRolesDao = sysRolesDao;
	}

	public BaseDao<SysRoleMembers> getSysRoleMemberDao() {
		return sysRoleMemberDao;
	}

	@Autowired
	public void setSysRoleMemberDao(BaseDao<SysRoleMembers> sysRoleMemberDao) {
		this.sysRoleMemberDao = sysRoleMemberDao;
	}

	public List<SysRoles> getSysRoles() {
		String hql = "from SysRoles";
		return sysRolesDao.find(hql);
	}

	public void saveSysRoles(SysRoles sr) {
		sysRolesDao.save(sr);
	}

	public void saveSysRoleMember(SysRoleMembers srm) {
		sysRoleMemberDao.save(srm);
	}

	public SysRoles getSysRoleByName(String roleName) {
		if (null == roleName) {
			return null;
		}
		SysRoles retsr = null;
		List<SysRoles> srs = getSysRoles();
		if (null != srs && srs.size() > 0) {
			for (SysRoles sr : srs) {
				if (roleName.equals(sr.getRoleName())) {
					retsr = sr;
				}
			}
		}
		return retsr;
	}

	public List<SysRoles> getSysRoleByMemberid(String memberid, String roleType) {
		String hql = "select sr from SysRoles sr, SysRoleMembers srm where srm.memberid=:memberid and srm.sysRoles=sr.id and srm.type=:type";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("memberid", memberid);
		params.put("type", roleType);
		return sysRolesDao.find(hql, params);
	}

	public boolean isSysRole(String memberid, String roleType, String roleName) {
		String hql = "select sr from SysRoles sr, SysRoleMembers srm where srm.memberid=:memberid and srm.sysRoles=sr.id and srm.type=:type and sr.roleName=:roleName";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("memberid", memberid);
		params.put("type", roleType);
		params.put("roleName", roleName);
		SysRoles sr = sysRolesDao.get(hql, params);
		if(null != sr) {
			return true;
		}
		return false;
	}

}
